package com.carkeeper.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.carkeeper.dto.CUserDTO;
import com.carkeeper.mapper.CUserMapper;
import com.carkeeper.pojo.CUserPO;
import com.carkeeper.service.CUserService;
import com.carkeeper.vo.CUserQueryParamVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.List;

@Service
public class CUserServiceImpl implements CUserService {
    @Autowired
    private CUserMapper userMapper;

    @Override
    public Boolean saveAndUpdate(CUserPO userPO) {
        if (!StringUtils.isEmpty(userPO.getPassword())) {
            BCryptPasswordEncoder passwordEncoder=new BCryptPasswordEncoder();
            userPO.setPassword(passwordEncoder.encode(userPO.getPassword()));
        }
        if (userPO.getId() != null) {
            userMapper.updateById(userPO);
        } else {
            userMapper.insert(userPO);
        }
        return true;
    }

    @Override
    public Boolean delete(Long id) {
        userMapper.deleteById(id);
        return true;
    }

    @Override
    public Page<CUserPO> findByPage(CUserQueryParamVo paramVo) {
        if (paramVo.getPage() == null) {
            paramVo.setPage(1);
        }
        if (paramVo.getPageSize() == null) {
            paramVo.setPageSize(10);
        }
        Page<CUserPO> page = new Page<>(paramVo.getPage(), paramVo.getPageSize());
        CUserPO paramPO = new CUserPO();
        BeanUtils.copyProperties(paramVo, paramPO);
        LambdaQueryWrapper<CUserPO> lambdaQueryWrapper = buildWrapper(paramPO, null);
        return userMapper.selectPage(page, lambdaQueryWrapper);
    }

    @Override
    public List<CUserPO> list(CUserDTO userDTO) {
        CUserPO paramsPO = new CUserPO();
        BeanUtils.copyProperties(userDTO, paramsPO);
        LambdaQueryWrapper<CUserPO> lambdaQueryWrapper = buildWrapper(paramsPO, userDTO.getIds());
        return userMapper.selectList(lambdaQueryWrapper);
    }

    @Override
    public CUserPO findById(Long id) {
        CUserPO po=userMapper.selectById(id);
        po.setPassword(null);
        return po;
    }

    /**
     * 构建查询包装器
     *
     * @param userPO 查询条件
     * @param ids    查询条件
     * @return 查询包装器
     */
    private LambdaQueryWrapper<CUserPO> buildWrapper(CUserPO userPO, List<Long> ids) {
        LambdaQueryWrapper<CUserPO> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        if (ids != null && ids.size() > 0) {
            lambdaQueryWrapper.in(CUserPO::getId, ids);
        }
        if (!StringUtils.isEmpty(userPO.getUsername())) {
            lambdaQueryWrapper.like(CUserPO::getUsername, userPO.getUsername());
        }
        if (!StringUtils.isEmpty(userPO.getNickname())) {
            lambdaQueryWrapper.like(CUserPO::getNickname, userPO.getNickname());
        }
        if (!StringUtils.isEmpty(userPO.getPhone())) {
            lambdaQueryWrapper.like(CUserPO::getPhone, userPO.getPhone());
        }
        if (!StringUtils.isEmpty(userPO.getSex())) {
            lambdaQueryWrapper.eq(CUserPO::getSex, userPO.getSex());
        }
        if (!StringUtils.isEmpty(userPO.getStatus())) {
            lambdaQueryWrapper.eq(CUserPO::getStatus, userPO.getStatus());
        }
        if (!StringUtils.isEmpty(userPO.getCityCode())) {
            lambdaQueryWrapper.eq(CUserPO::getCityCode, userPO.getCityCode());
        }
        if (!StringUtils.isEmpty(userPO.getSource())) {
            lambdaQueryWrapper.eq(CUserPO::getSource, userPO.getSource());
        }
        if (!StringUtils.isEmpty(userPO.getApp())) {
            lambdaQueryWrapper.eq(CUserPO::getApp, userPO.getApp());
        }
        lambdaQueryWrapper.orderByDesc(CUserPO::getCreateTime);
        return lambdaQueryWrapper;
    }
}
